using System;
using TMPro;
using UnityEngine;

public class GonerMenu_Settings_Keybind : MonoBehaviour
{
	[Header("-= References =-")]
	[SerializeField]
	private GonerMenu_Settings_Element AssosciatedElement;

	[SerializeField]
	private TextMeshProUGUI KeybindText;

	[SerializeField]
	private bool IgnoreGonerMenuOpenState;

	[Header("Only used for the main menu settings, keep null to use global reference")]
	[SerializeField]
	private GonerMenu_Settings IgnoredGonerSettingsManager;

	[Header("-= Settings =-")]
	[Header("Is saved as Setting_<KeyBinding>")]
	[SerializeField]
	private string KeyBinding;

	[SerializeField]
	private KeyCode DefaultKeyBinding;

	private int currentKeyCode;

	private void OnEnable()
	{
		LoadKeybinding();
	}

	private void LoadKeybinding()
	{
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Expected I4, but got Unknown
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		currentKeyCode = PlayerPrefs.GetInt("Setting_" + KeyBinding, (int)DefaultKeyBinding);
		((TMP_Text)KeybindText).text = ((object)(KeyCode)currentKeyCode/*cast due to .constrained prefix*/).ToString();
	}

	private void Update()
	{
		//IL_0050: Unknown result type (might be due to invalid IL or missing references)
		//IL_0055: Unknown result type (might be due to invalid IL or missing references)
		//IL_0056: Unknown result type (might be due to invalid IL or missing references)
		//IL_006c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0072: Expected I4, but got Unknown
		if (!GonerMenu.Instance.GonerMenuOpen && !IgnoreGonerMenuOpenState)
		{
			AssosciatedElement.CurrentlySelected = false;
		}
		if (!AssosciatedElement.CurrentlySelected)
		{
			return;
		}
		foreach (KeyCode value in Enum.GetValues(typeof(KeyCode)))
		{
			if (Input.GetKeyDown(value) && !PlayerInput.HoldingAnyImportantKeys())
			{
				currentKeyCode = (int)value;
				((TMP_Text)KeybindText).text = ((object)value/*cast due to .constrained prefix*/).ToString();
				PlayerPrefs.SetInt("Setting_" + KeyBinding, currentKeyCode);
				if ((Object)(object)IgnoredGonerSettingsManager != (Object)null)
				{
					IgnoredGonerSettingsManager.CancelOutOfCurrentSetting();
				}
				else
				{
					GonerMenu_Settings.Instance.CancelOutOfCurrentSetting();
				}
				SettingsManager.Instance.ApplySettings();
				Debug.Log((object)("New keycode set: " + ((object)value/*cast due to .constrained prefix*/).ToString()));
				PlayerInput.LoadKeybinding();
			}
		}
	}

	public void UpdateKeybindText()
	{
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0026: Expected I4, but got Unknown
		((TMP_Text)KeybindText).text = string.Format("{0}", (object)(KeyCode)PlayerPrefs.GetInt("Setting_" + KeyBinding, (int)DefaultKeyBinding));
	}
}
